package com.caimiao.miao.page.master;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;

import com.caimiao.miao.R;
import com.caimiao.miao.bean.TicketOut;
import com.caimiao.miao.interfaces.BaseActivity;
import com.caimiao.miao.page.master.device.DeviceFragment;
import com.caimiao.miao.page.master.system.lottery.UsbHostActivity;
import com.caimiao.miao.page.master.system.server.ServerFragment;
import com.caimiao.miao.page.master.storage.enter.EnterFragment;
import com.caimiao.miao.page.ticket.TicketActivity;

import java.util.Random;

public class MasterActivity extends BaseActivity<IMasterContact.Presenter> implements IMasterContact.View, View.OnClickListener {
    private FragmentManager mFragmentManager;
    private Fragment mCurFragment;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_master;
    }

    @Override
    protected void initUI() {
        setTitle(R.string.title_manage);
    }

    @Override
    protected void initLogic() {
        LinearLayout mLlMenu = findViewById(R.id.masterLlMenu);
        for (int i = 0; i < mLlMenu.getChildCount(); i++) {
            mLlMenu.getChildAt(i).setOnClickListener(this::onClick);
        }
        mFragmentManager = getSupportFragmentManager();
        mCurFragment = new DeviceFragment();
        mFragmentManager.beginTransaction()
                .add(R.id.masterFlFragment, mCurFragment, DeviceFragment.TAG)
                .commit();
        new MasterPresenter(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.masterTvDevice:
            case R.id.masterTvInfo:
            case R.id.masterTvStorage:
            case R.id.masterTvTicket:
            case R.id.masterTvAdvert:
            case R.id.masterTvSystem:
                mPresenter.replaceFragment(mFragmentManager, v.getId());
                break;
            case R.id.manageBtnBluetooth:
//                startActivity(new Intent(this, BluetoothActivity.class));
                startActivity(new Intent(this, UsbHostActivity.class));
                break;
        }


    }

    @Override
    public void replace(Fragment fragment, boolean isCreate, String tag, boolean canBack) {
        if (fragment == null || fragment.equals(mCurFragment)) return;
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        if (isCreate) {
            ft.add(R.id.masterFlFragment, fragment, tag);
        }
        ft.replace(R.id.masterFlFragment, fragment, tag);
        if (canBack) {
            ft.addToBackStack(tag);
        }
        ft.commit();

        mCurFragment = fragment;
    }

    public <T> void replaceByChild(Class<T> tClass) {
        if (tClass == null) return;
        mPresenter.setFragment(mFragmentManager, tClass, true);
    }

    public void popBackFragment() {
        if (mFragmentManager.popBackStackImmediate())
            mFragmentManager.popBackStack();
    }
}
